package com.example.springbatchsample.config;

import com.example.springbatchsample.database.Person;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Collections;

/**
 * 从数据库中读取
 *
 * @author Yunkai Luo
 * @since 2022/1/28
 **/
@Configuration
public class JdbcBatchConfiguration {

    /**
     * 创建任务对象的对象
     */
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    /**
     * 任务的执行由Step决定,
     * 这个用来创建Step
     */
    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    @StepScope
    public JdbcPagingItemReader<Person> jdbcReader(DataSource dataSource) {
        MySqlPagingQueryProvider provider=new MySqlPagingQueryProvider();
        provider.setSelectClause("first_name, last_name");
        provider.setFromClause("people");
        provider.setSortKeys(Collections.singletonMap("first_name", Order.ASCENDING));

        //从csv文件读取两列信息作为名字
        return new JdbcPagingItemReaderBuilder<Person>()
                .name("personJdbcItemReader")
                .dataSource(dataSource)
                .fetchSize(2)
                .queryProvider(provider)
                .rowMapper((rs, rowNum) ->
                        new Person(
                                rs.getString(1),
                                rs.getString(2)))
                .build();
    }

    /**
     * 多文件读取，
     * 可以将几个文件的数据整合到一起
     * @param jdbcReader jdbc不可以
     * @param csvReader csv的可以
     * @return 多文件读取
     */
    @Bean
    @StepScope
    public ItemReader<Person> multiReader(ItemReader<Person> jdbcReader,
            FlatFileItemReader<Person> csvReader) {
        return new MultiResourceItemReaderBuilder<Person>()
                .name("multiReader")
                .delegate(csvReader)
                .build();
    }


    /**
     * 从数据库中取出用户并处理
     *
     * @param itemReaderDbStep
     * @return
     */
    @Bean
    public Job handleUserJob(Step itemReaderDbStep) {
        return jobBuilderFactory
                .get("handleUserJob")
                .incrementer(new RunIdIncrementer())
                .flow(itemReaderDbStep)
                .end()
                .build();
    }

    @Bean
    public MyItemWriter<Person> myPersonItemWriter() {
        return new MyItemWriter<>();
    }

    @Bean
    public Step itemReaderDbStep(ItemReader<Person> jdbcReader,
            MyItemWriter<Person> myPersonItemWriter) {
        return stepBuilderFactory.get("itemReaderDbStep")
                .<Person, Person>chunk(2)
                .reader(jdbcReader)
                .writer(myPersonItemWriter)
                .build()
                ;
    }

}
